package com.markus.code.剑指offer;

import java.util.Stack;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/31 13:32
 * todo: 栈的压入、弹出序列
 */
public class ValidateStackSequences {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed == null || popped == null){
            return false;
        }
        if (pushed.length == 0 && popped.length == 0){
            return true;
        }
        if (pushed.length == 0 || popped.length == 0){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        stack.push(pushed[0]);
        int pushIndex = 1;
        int popIndex = 0;
        while(pushIndex < pushed.length){
            while(!stack.isEmpty() && stack.peek() == popped[popIndex]){
                stack.pop();
                popIndex++;
            }
            stack.push(pushed[pushIndex++]);
        }
        while(!stack.isEmpty() && stack.peek() == popped[popIndex]){
            stack.pop();
            popIndex++;
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        int []pushed = {};
        int []popped = {};
        ValidateStackSequences demo = new ValidateStackSequences();
        System.out.println(demo.validateStackSequences(pushed,popped));
    }
}
